using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace SmartLink.Infrastructure
{
    public class MyGenerateSheet<T> : GenerateSheet<T>
    {
        public MyGenerateSheet(List<T> _dataSource)
            : base(_dataSource, null, string.Empty, true)
        {
        }

        public MyGenerateSheet(List<T> _dataSource, string sheetName)
            : base(_dataSource, null, sheetName, true)
        {
        }

        protected override List<ColumnsMapping> InitializeColumnHeadData()
        {
            try
            {
                int columnIndex = 0;
                List<PropertyInfo> propertyList = GetObjectPropertyList();
                List<ColumnsMapping> columnsList = new List<ColumnsMapping>();
                ColumnsMapping curColumnsMapping = null;
                foreach (PropertyInfo propertyInfo in propertyList)
                {

                    curColumnsMapping = new ColumnsMapping();
                    ExcelFieldAttribute excelFieldAttribute = (ExcelFieldAttribute)propertyInfo.GetCustomAttribute(typeof(ExcelFieldAttribute));
                    bool needExport = excelFieldAttribute == null ? false : true;
                    if (excelFieldAttribute != null)
                    {
                        curColumnsMapping.ColumnsData = propertyInfo.Name;
                        curColumnsMapping.ColumnsText = needExport ? excelFieldAttribute.Name : propertyInfo.Name;
                        curColumnsMapping.ColumnsIndex = excelFieldAttribute.Sort - 1;
                    }
                    else
                        continue;

                    curColumnsMapping.IsTotal = false;
                    curColumnsMapping.Width = 15;
                    columnIndex++;
                    columnsList.Add(curColumnsMapping);
                }
                return columnsList.OrderBy(one => one.ColumnsIndex).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }

    public class ExcelFieldAttribute : Attribute
    {
        public string Name { get; set; }
        public int Sort { get; set; }
    }
}
